package com.lcz.manage.util.event.listener.strategy;

import java.util.Objects;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.lcz.manage.util.SpringContextUtils;
import com.lcz.manage.util.event.event.Event;
import com.lcz.manage.util.event.event.strategy.CommonStrategyDeleteEvent;
import com.lcz.manage.util.event.listener.Listener;
import com.lcz.manage.util.event.publisher.EventPublisher;

/**
 * @author xingyongjiang@yunrong.cn
 * @version V3.0
 * @since 2021/4/19 15:04
 */
@Component
public class CommonStrategyDeleteListener implements Listener {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    @Qualifier("syncApplicationEventPublisher")
    private EventPublisher eventPublisher;

    @Override
    @PostConstruct
    public void register() {
        eventPublisher.setListener(this);
    }

    @Override
    public boolean acceptEvent(Event event) {
        return event instanceof CommonStrategyDeleteEvent;
    }

    @Override
    public void handleEvent(Event event) {
        CommonStrategyDeleteEvent deleteEvent = (CommonStrategyDeleteEvent) event;
        if (Objects.nonNull(deleteEvent.getConfigKey())) {
            SpringContextUtils.invokeBeanMethod(this.applicationContext.getBean("testService"), "delete",
                deleteEvent.getConfigKey());
        }
    }

}